package com.peng.ivos.geofence.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.peng.ivos.base.response.JsonResult;
import com.peng.ivos.base.response.StatusCode;
import com.peng.ivos.base.util.CacheUtils;
import com.peng.ivos.geofence.mapper.GeofenceMapper;
import com.peng.ivos.geofence.pojo.dto.GeofenceParam;
import com.peng.ivos.geofence.pojo.dto.GeofenceQuery;
import com.peng.ivos.geofence.pojo.entity.Geofence;
import com.peng.ivos.geofence.pojo.vo.GeofenceVO;
import com.peng.ivos.geofence.service.GeofenceService;
import com.peng.ivos.vehicle.mapper.VehicleMapper;
import com.peng.ivos.vehicle.pojo.dto.VehicleQuery;
import com.peng.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 电子围栏业务层实现类
 * @author： Peng
 * @date： 2024/5/23
 * @version: 1.0
 */
@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public JsonResult getGeofenceList(GeofenceQuery geofenceQuery) {
        if (geofenceQuery == null){
            return JsonResult.error(StatusCode.DATA_UNEXISTS,"查询参数不能为空");
        }
        // 查询电子围栏列表
        List<GeofenceVO> geofenceList = geofenceMapper.seletGeofenceByExample(geofenceQuery);
        log.debug("geofenceList:{}",geofenceList);
        // 查询电子围栏数量
        VehicleQuery vehicleQuery = new VehicleQuery();
        int availableCount = 0;
        for (GeofenceVO geofenceVO : geofenceList) {
            VehicleQuery vehicleQuery1 = new VehicleQuery();
            vehicleQuery1.setGeofenceId(geofenceVO.getId());
            Integer bindCount = vehicleMapper.countGeofenceByExample(vehicleQuery1);
            geofenceVO.setTotalNum(bindCount);
            List<VehicleVO> vehicleVOList = vehicleMapper.selectByExample(vehicleQuery1);
            log.debug("vehicleVOList:{}",vehicleVOList);
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (Objects.equals(vehicleVO.getStatus(), "1")){
                    availableCount++;
                }
            }
            geofenceVO.setAvailableNum(availableCount);
            log.debug("修改后的geofenceVO:{}",geofenceVO);
            availableCount = 0;
        }


        return JsonResult.success("查询成功",geofenceList);
    }

    @Override
    public JsonResult saveGeofence(GeofenceParam geofenceParam) {
        if (geofenceParam == null){
            return JsonResult.error(StatusCode.DATA_UNEXISTS,"插入围栏参数不能为空");
        }
        Geofence geofence = Geofence.builder()
                .id(geofenceParam.getId())
                .name(geofenceParam.getName())
                .status(geofenceParam.getStatus())
                .build();
        HashMap<String, String> position = geofenceParam.getPosition();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //该方法当传入参数为空时会抛出异常，所以需要进行异常捕获处理
            String positionStr = objectMapper.writeValueAsString(position);
            if (geofenceParam.getId()!=null){
//                geofence.setPosition(positionStr);
                return geofenceMapper.update(geofence) > 0 ? JsonResult.success("更新围栏成功") : JsonResult.error("更新围栏失败");
            } else {
                geofence.setPosition(positionStr);
                geofence.setStatus("0");
                geofence.setCreateTime(new Date());
                return geofenceMapper.insert(geofence) > 0 ? JsonResult.success("新增围栏成功") : JsonResult.error("新增围栏失败");
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JsonResult deleteGeofence(Long id) {
        if (id == null){
            return JsonResult.error(StatusCode.DATA_UNEXISTS,"围栏id不能为空");
        }
        return geofenceMapper.deleteById(id) > 0 ? JsonResult.success("删除围栏成功") : JsonResult.error("删除围栏失败");
    }
}
